home *** CD-ROM | disk | FTP | other *** search
- Path: ra.cgd.ucar.edu!rosinski
- From: rosinski@ra.cgd.ucar.edu (Jim Rosinski)
- Newsgroups: comp.lang.c
- Subject: Why does this work?
- Date: 19 Feb 1996 00:11:36 GMT
- Organization: Climate and Global Dynamics Division/NCAR, Boulder, CO
- Message-ID: <4g8f7o$adt@ncar.ucar.edu>
- NNTP-Posting-Host: ra.cgd.ucar.edu
-
-
- Could someone please explain why the following code works? In particular, I
- am perplexed as to why "sub1" and "sub2" declared as elements of "cmndtable"
- are valid "pointers to function returning void" (i.e. see the definition of
- "cmndstruct"). When executed, the net result is indeed to invoke two
- functions, "sub1", and "sub2" (tested on Solaris, AIX, and UNICOS).
-
- Feel free to post, but please also email as I am not a regular reader of this
- group. Thanks.
-
- Jim Rosinski
- rosinski@ncar.ucar.edu
-
-
- #include <stdio.h>
- main()
- {
- void sub1(), sub2();
-
- struct cmndstruct {
- void (*funcnam)();
- };
-
- struct cmndstruct cmndtable[] = {
- sub1,
- sub2,
- NULL
- };
- struct cmndstruct *cmndptr;
-
- for (cmndptr = cmndtable; *(cmndptr->funcnam) != NULL; cmndptr++) {
- (*(cmndptr->funcnam))();
- }
- exit(0);
- }
-
- void sub1()
- {
- printf("Inside sub1\n");
- }
-
- void sub2()
- {
- printf("Inside sub2\n");
- }
-
-